package marchsoft.modules.selfFish.pisciculture.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.selfFish.pisciculture.entity.dto.CommunityCommunicationDto;
import marchsoft.modules.selfFish.pisciculture.service.CommunityCommunicationService;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**社区交流
 * @title FishController
 * @description 社区交流
 * @author Lenovo
 * @version 1.0
 * @create 2024-07-15 16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/community_communication")
@Api(tags = "养鱼管理：社区交流")
public class CommunityCommunicationController {

    @Autowired
    private CommunityCommunicationService communityCommunicationService;

    private static final String ENTITY_NAME = "communityCommunication";

    @GetMapping
    @ApiOperation("获社区交流列表")
    @PreAuthorize("@smpe.check('communityCommunication:list')")
    public Result<Object> query(String content,
                                @RequestParam(value ="pageNum",defaultValue = "1")Long pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Long pageSize ){
        log.info(StrUtil.format("【获社区交流列表 /api/communityCommunication】操作人id：{}，社区交流查询条件 communityCommunicationName：{}，分页pageNum：{}", SecurityUtils.getCurrentUserId(),content, pageNum));
        return communityCommunicationService.getCommunityCommunicationList(content,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation("新增社区交流")
    @PreAuthorize("@smpe.check('communityCommunication:add')")
    public Result<Object> create(@RequestBody CommunityCommunicationDto communityCommunicationDto){
        log.info(StrUtil.format("【新增社区交流 /api/communityCommunication】操作人id：{}，新增社区交流实体：{}", SecurityUtils.getCurrentUserId(), communityCommunicationDto));
        if (ObjectUtil.isNotNull(communityCommunicationDto.getId())) {
            log.error(StrUtil.format("【新增社区交流失败】操作人id：{}，新增社区交流实体默认id应该为空，dept：{}", SecurityUtils.getCurrentUserId(), communityCommunicationDto));
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return communityCommunicationService.create(communityCommunicationDto);
    }

    @DeleteMapping
    @ApiOperation("删除社区交流")
    @PreAuthorize("@smpe.check('communityCommunication:del')")
    public Result<Object> delete(@RequestBody Set<Long> ids){
        log.info(StrUtil.format("【删除社区交流 /api/communityCommunication】操作人id：{}，被删除社区交流id集：{}", SecurityUtils.getCurrentUserId(), ids.toString()));
        return communityCommunicationService.delete(ids);
    }

    @PutMapping
    @ApiOperation("修改社区交流")
    @PreAuthorize("@smpe.check('communityCommunication:edit')")
    public Result<Object> update(@RequestBody CommunityCommunicationDto communityCommunicationDto){
        log.info(StrUtil.format("【修改社区交流 /api/communityCommunication】操作人id：{}，被修改社区交流id：{}", SecurityUtils.getCurrentUserId(), communityCommunicationDto.getId()));
        return communityCommunicationService.update(communityCommunicationDto);
    }

    @ApiOperation("导出社区交流数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@smpe.check('communityCommunication:list')")
    public void download(HttpServletResponse response, String content) throws IOException {
        log.info(StrUtil.format("【查询社区交流 /api/communityCommunication】操作人id：{}，社区交流查询条件 fishName：{}", SecurityUtils.getCurrentUserId(), content));
        communityCommunicationService.download(content,response);
    }

    @PutMapping("/addThumbsUpNum")
    @ApiOperation("添加点赞数")
    @PreAuthorize("@smpe.check('communityCommunication:thumbsUp')")
    public Result<Object> addThumbsUpNum(@RequestBody CommunityCommunicationDto communication){
        log.info(StrUtil.format("【添加社区交流点赞 /api/addThumbsUpNum】操作人id：{}，被查询社区交流id：{}", SecurityUtils.getCurrentUserId(), communication.getId()));
        return Result.success(communityCommunicationService.addThumbsUp(communication.getId()));
    }

    @PutMapping("/addViewNum")
    @ApiOperation("添加查看数")
    @PreAuthorize("@smpe.check('communityCommunication:view')")
    public Result<Object> addViewNum(@RequestBody CommunityCommunicationDto communication){
        log.info(StrUtil.format("【添加社区交流查看数 /api/addViewNum】操作人id：{}，被查询社区交流id：{}", SecurityUtils.getCurrentUserId(), communication.getId()));
        return Result.success(communityCommunicationService.addViewNum(communication.getId()));
    }

    @PutMapping("/deleteThumbsUpNum")
    @ApiOperation("删除点赞数")
    @PreAuthorize("@smpe.check('communityCommunication:thumbsUp')")
    public Result<Object> deleteThumbsUp(@RequestBody CommunityCommunicationDto communication){
        log.info(StrUtil.format("【删除社区交流点赞数 /api/communityCommunication】操作人id：{}，被查询社区交流id：{}", SecurityUtils.getCurrentUserId(), communication.getId()));
        return Result.success(communityCommunicationService.deleteThumbsUp(communication.getId()));
    }
}
